package com.alvin.mbp.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alvin.mbp.entity.Gender;
import com.alvin.mbp.entity.Marchandise;
import com.alvin.mbp.entity.PointMember;
import com.alvin.mbp.service.PointMemberService;
import com.alvin.mbp.service.PointService;

@Controller
public class LoginAction {

    @Autowired
    private PointMemberService pointMemberService;
    @Autowired
    private PointService pointService;
    
    @RequestMapping("/")
    public String index(HttpServletRequest request) {
        return "/index";
    }

    @RequestMapping(value = "/login.htm", method = RequestMethod.GET)
    public String login(HttpServletRequest request) {
        Object member = request.getSession().getAttribute("MEMBER");
        if (member != null) {
            return "redirect:/home.htm";
        } else {
            return "/index";
        }
    }
    
    @RequestMapping(value = "/login.htm", method = RequestMethod.POST)
    public String login(Model model,  String account, String iCode, HttpServletRequest request) {
    	PointMember member = pointMemberService.findByPropertyOne("mobile", account);
    	if(member == null) {
    		Map<String, Object> goods = pointService.searchPointGoods(iCode);
    		Marchandise marchandise = (Marchandise) goods.get("m");
    		
    		if("".equals(iCode) || goods.isEmpty() || marchandise == null){
        		model.addAttribute("msg", "积分码无效!");
        		model.addAttribute("mobile", account);
        		return "/index"; 			
    		}
			member = new PointMember();
			member.setMobile(account);
			member.setUserName(account);
			member.setBalance(0);
			member.setGender(Gender.MALE.getDisplayName());
			pointMemberService.save(member);
			request.getSession().setAttribute("MEMBER", pointMemberService.findByProperty("mobile", member.getMobile()).get(0));
			
			return "redirect:/points/charge.htm?icode="+iCode;
		}else{
			request.getSession().setAttribute("MEMBER", pointMemberService.findById(member.getId()));
    		Map<String, Object> goods = pointService.searchPointGoods(iCode);
    		Marchandise marchandise = (Marchandise) goods.get("m");
    		
			if(!goods.isEmpty() && marchandise != null){
	    		iCode = iCode.trim().replace(" ", "");
	    		return "redirect:/points/charge.htm?icode="+iCode;				
			}else if("".equals(iCode)){
	    		return "redirect:/points/chargeList.htm";
			}else{
	    		model.addAttribute("msg", "积分码无效!");
	    		model.addAttribute("mobile", account);
	    		return "/index";
			}			
		}
    }

    @RequestMapping(value = "/logout.htm", method = RequestMethod.GET)
    public String logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:/login.htm";
    }

}
